package com.dynamicprogram;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

public class Solution1218 {
    public int longestSubsequence(int[] arr, int difference) {
        int[] dp = new int[arr.length];
        Arrays.fill(dp, 1);
        int res = 1;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(arr[0], 1);
        for (int i = 1; i < arr.length; i++) {
            int tmp = map.getOrDefault(arr[i] - difference, -1);
            if (tmp != -1) {
                dp[i] = tmp + 1;
            }
            int tmp2 = map.getOrDefault(arr[i], -1);
            tmp2 = Math.max(dp[i], tmp2);
            map.put(arr[i], tmp2);
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}
